package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.bo.ErpUsdExchangeBo;
import com.erp.gather.domain.vo.ErpUsdExchangeVo;
import com.erp.gather.mapper.ErpUsdExchangeMapper;
import com.erp.gather.service.IErpUsdExchangeService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.gather.api.domain.ErpUsdExchange;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 美元汇率Service业务层处理
 *
 * @author erp
 * @date 2023-05-06
 */
@RequiredArgsConstructor
@Service
public class ErpUsdExchangeServiceImpl implements IErpUsdExchangeService {

    private final ErpUsdExchangeMapper baseMapper;

    /**
     * 查询美元汇率
     */
    @Override
    public ErpUsdExchangeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询美元汇率列表
     */
    @Override
    public TableDataInfo<ErpUsdExchangeVo> queryPageList(ErpUsdExchangeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpUsdExchange> lqw = buildQueryWrapper(bo);
        Page<ErpUsdExchangeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询美元汇率列表
     */
    @Override
    public List<ErpUsdExchangeVo> queryList(ErpUsdExchangeBo bo) {
        LambdaQueryWrapper<ErpUsdExchange> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpUsdExchange> buildQueryWrapper(ErpUsdExchangeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpUsdExchange> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ErpUsdExchange::getName, bo.getName());
        lqw.eq(bo.getFBuyPri() != null, ErpUsdExchange::getFBuyPri, bo.getFBuyPri());
        lqw.eq(bo.getMBuyPri() != null, ErpUsdExchange::getMBuyPri, bo.getMBuyPri());
        lqw.eq(bo.getFSellPri() != null, ErpUsdExchange::getFSellPri, bo.getFSellPri());
        lqw.eq(bo.getMSellPri() != null, ErpUsdExchange::getMSellPri, bo.getMSellPri());
        lqw.eq(bo.getBankConversionPri() != null, ErpUsdExchange::getBankConversionPri, bo.getBankConversionPri());
        lqw.eq(StringUtils.isNotBlank(bo.getDate()), ErpUsdExchange::getDate, bo.getDate());
        lqw.eq(StringUtils.isNotBlank(bo.getTime()), ErpUsdExchange::getTime, bo.getTime());
        return lqw;
    }

    /**
     * 新增美元汇率
     */
    @Override
    public Boolean insertByBo(ErpUsdExchangeBo bo) {
        ErpUsdExchange add = BeanUtil.toBean(bo, ErpUsdExchange.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改美元汇率
     */
    @Override
    public Boolean updateByBo(ErpUsdExchangeBo bo) {
        ErpUsdExchange update = BeanUtil.toBean(bo, ErpUsdExchange.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpUsdExchange entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除美元汇率
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Integer insertUsdExchange(ErpUsdExchange usdExchange) {
        return baseMapper.insert(usdExchange);
    }

//    @Override
//    public ErpUsdExchange queryByCreateTime() {
//        return baseMapper.queryByCreateTime();
//    }

    @Override
    public BigDecimal getNewUsdExchange(String code) {
        if (code != null) {
            if ("USD".equalsIgnoreCase(code)) {
                code = "美元";
            } else if ("CUR".equalsIgnoreCase(code)) {
                code = "欧元";
            } else if ("CNY".equalsIgnoreCase(code)) {
                return new BigDecimal("100");
            }
        }else { // 不传币种默认美元
            code = "美元";
        }
        ErpUsdExchange erpUsdExchange = baseMapper.queryByCreateTime(code);
        return erpUsdExchange.getBankConversionPri();
    }

    @Override
    public BigDecimal getExchangeByTime(String time, String currencyCode) {
        if (StringUtils.isEmpty(currencyCode)) {
            return BigDecimal.ZERO;
        }
        if ("USD".equalsIgnoreCase(currencyCode)) {
            currencyCode =  "美元";
        }else if ("CUR".equalsIgnoreCase(currencyCode)) {
            currencyCode = "欧元";
        }else {
            return new BigDecimal(100);
        }
        ErpUsdExchange erpUsdExchange = baseMapper.selectOne(new LambdaQueryWrapper<ErpUsdExchange>()
                .le(ErpUsdExchange::getCreateTime, time)
                        .eq(ErpUsdExchange::getName,currencyCode)
                .orderByDesc(ErpUsdExchange::getCreateTime).last("limit 1"));
        if (erpUsdExchange == null) {
            erpUsdExchange = baseMapper.selectOne(new LambdaQueryWrapper<ErpUsdExchange>().eq(ErpUsdExchange::getName,currencyCode)
                    .orderByDesc(ErpUsdExchange::getCreateTime).last("limit 1"));
        }
        return erpUsdExchange.getBankConversionPri();
    }

    @Override
    public BigDecimal getNewUsdExchange() {
        String code = "美元";
//        ErpUsdExchange erpUsdExchange = baseMapper.queryByCreateTime(code);
        ErpUsdExchange erpUsdExchange = baseMapper.selectOne(
                new LambdaQueryWrapper<ErpUsdExchange>()
                        .eq(ErpUsdExchange::getName,"美元")
                        .orderByDesc(ErpUsdExchange::getCreateTime)
                        .last("limit 1")
        );
        return erpUsdExchange.getBankConversionPri();
    }
}
